import { ref, onMounted } from 'vue'
import { defineStore } from 'pinia'
import { getCurrent } from '@/apis/city'
import { getWeather, getCurWeather } from '@/apis/weather'

export const useLocalWeatherStore = defineStore('LocalWeather', () => {
    const LocalCity = ref('')
    const LocalWeather = ref('')
    const curWeather = ref('')

    const getLocalCity = async () => {
        const res = await getCurrent()
        LocalCity.value = res.data
    }
    const getCurWeatherData = async (city) => {
        const res = await getCurWeather(city)
        return res.data
    }
    const getWeatherData = async (city) => {
        const res = await getWeather(city)
        return res.data
    }
    onMounted(() => {
        getLocalCity().then(() => {
            getCurWeatherData(LocalCity.value.adcode).then(res => {
                curWeather.value = res.lives[0]
            })
            getWeatherData(LocalCity.value.adcode).then(res => {
                LocalWeather.value = res.forecasts[0].casts
            })
        })
    })
    return {
        LocalCity,
        LocalWeather,
        curWeather
    }
})
